package com.bestattempt.utility.bind.initializers.hibernateImpl;

import com.bestattempt.utility.bind.initializers.defaultImpl.ObjectInitializer;
import com.bestattempt.utility.bind.initializers.defaultImpl.MapInitializer;
import com.bestattempt.utility.bind.InitializerException;
import static com.bestattempt.utility.reflection.ReflectionUtil.getFieldValue;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import org.hibernate.collection.PersistentList;
import org.hibernate.collection.PersistentMap;

/**
 * TODO: Document me
 *
 * @author Ganeshji Marwaha
 * @since Nov 4, 2006
 */
public class HibernateMapInitializer extends MapInitializer {

    public Object initialize(Object object, Field field, String index) {
        Map map = (Map) getFieldValue(object, field);

        if (map instanceof PersistentMap) {
            if( !((PersistentMap) map).wasInitialized() ) {
                 throw new InitializerException("Map was not initialized by the ORM");
            }
        }

        return super.initialize(object, field, index);
    }

}
